package com.showde.shoppingmall.business.logic;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;

import com.kim.net.NetEntity;
import com.showde.shoppingmall.business.AddressBusiness;
import com.showde.shoppingmall.business.NetCallbackImplBase.Notify;
import com.showde.shoppingmall.db.AddressDBCommand;
import com.showde.shoppingmall.domain.Address;

public class AddressLogic {

	public static void fetchAddress(final Context context,
			final UICallbackInterface uiCallbackInterface) {
		AddressBusiness addressBusiness = new AddressBusiness(new Notify() {

			@Override
			public void notifyNetCallback(NetEntity netEntity) {
				JSONObject jsonObject = netEntity.result;
				try {
					String returnCode = jsonObject.getString("ReturnCode");
					if ("0".equals(returnCode)) {
						JSONArray addressArray = jsonObject
								.getJSONArray("List");
						JSONObject addressJSON = null;
						List<Address> addresses = new ArrayList<Address>();
						Address address = null;
						for (int i = 0; i < addressArray.length(); i++) {
							addressJSON = addressArray.getJSONObject(i);
							address = new Address();

							address.setRecipientId(addressJSON.getString("id"));
							address.setRecipient(addressJSON
									.getString("toname"));
							address.setAddressContent(addressJSON
									.getString("toaddr"));
							address.setPhoneNum(addressJSON
									.getString("tomobile"));
							// address.setHomeNum(addressJSON.getString(""));
							address.setPostcode(addressJSON
									.getString("postcode"));

							addresses.add(address);
						}

						AddressDBCommand addressDBCommand = new AddressDBCommand(
								context);
						addressDBCommand.add(addresses);

						uiCallbackInterface.onSuccess(null);

						return;

					}
				} catch (Exception e) {
					uiCallbackInterface.onError(e.getMessage());
				}

			}

			@Override
			public void notifyError(int httpResponseCode, String errorMsg) {
				uiCallbackInterface.onError(errorMsg);
			}
		});

		addressBusiness.doBusiness();
	}

	public static void addAddress(Address recipientAddress,
			final UICallbackInterface uiCallbackInterface) {

		if (recipientAddress == null) {
			uiCallbackInterface
					.onError("Parameter Address NullpointException.");
			return;
		}

		String recipientName = recipientAddress.getRecipient();
		String recipientAddressContent = recipientAddress.getAddressContent();
		String recipientMobile = recipientAddress.getPhoneNum();
		String recipientPostCode = recipientAddress.getPostcode();

		AddressBusiness addressBusiness = new AddressBusiness(recipientName,
				recipientAddressContent, recipientMobile, recipientPostCode,
				new Notify() {

					@Override
					public void notifyNetCallback(NetEntity netEntity) {
						JSONObject jsonObject = netEntity.result;
						try {
							if (jsonObject != null) {
								String returnCode = jsonObject
										.getString("ReturnCode");
								if ("0".equals(returnCode)) {
									uiCallbackInterface.onSuccess(null);
									return;
								}
							}
						} catch (Exception e) {
							uiCallbackInterface.onError(e.getMessage());
							return;
						}
						uiCallbackInterface.onError("unkown error.");
					}

					@Override
					public void notifyError(int httpResponseCode,
							String errorMsg) {

					}
				});
		addressBusiness.doBusiness();
	}

	public static final void updateAddress(final Context context,
			final Address recipientAddress,
			final UICallbackInterface uiCallbackInterface) {
		String recipientName = recipientAddress.getRecipient();
		String recipientAddressContent = recipientAddress.getAddressContent();
		String recipientMobile = recipientAddress.getPhoneNum();
		String recipientPostCode = recipientAddress.getPostcode();
		String recipientAddressid = recipientAddress.getRecipientId();

		AddressBusiness addressBusiness = new AddressBusiness(recipientName,
				recipientAddressContent, recipientMobile, recipientPostCode,
				recipientAddressid, new Notify() {

					@Override
					public void notifyNetCallback(NetEntity netEntity) {
						try {
							JSONObject result = netEntity.result;
							String returnCode = result.getString("ReturnCode");
							if ("0".equals(returnCode)) {
								AddressDBCommand addressDBCommand = new AddressDBCommand(
										context);
								addressDBCommand.add(recipientAddress);
								uiCallbackInterface.onSuccess(null);
								return;
							}
							uiCallbackInterface.onError("");
						} catch (Exception e) {
							if (e != null) {
								uiCallbackInterface.onError(e.getMessage());
							}
						}
						uiCallbackInterface.onError("unkown");
					}

					@Override
					public void notifyError(int httpResponseCode,
							String errorMsg) {
						uiCallbackInterface.onError(errorMsg);
					}
				});
		addressBusiness.doBusiness();
	}

	public static final void deleteAddress(final Context context,
			final String addressId,
			final UICallbackInterface uiCallbackInterface) {
		AddressBusiness addressBusiness = new AddressBusiness(addressId,
				new Notify() {

					@Override
					public void notifyNetCallback(NetEntity netEntity) {
						try {
							JSONObject jsonObject = netEntity.result;
							String returnCode = jsonObject
									.getString("ReturnCode");

							if ("0".equals(returnCode)) {
								AddressDBCommand addressDBCommand = new AddressDBCommand(
										context);
								addressDBCommand.delete(addressId);
								uiCallbackInterface.onSuccess(null);
								return;
							}
						} catch (Exception e) {
							uiCallbackInterface.onError(e.getMessage());
						}
					}

					@Override
					public void notifyError(int httpResponseCode,
							String errorMsg) {
						uiCallbackInterface.onError(errorMsg);
					}
				});
		addressBusiness.doBusiness();
	}
}
